{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-01T11:42:50.231185Z",
     "start_time": "2024-12-01T11:42:43.211809Z"
    }
   },
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torchvision\n",
    "from torchvision import transforms\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:42:51.600346Z",
     "start_time": "2024-12-01T11:42:51.470966Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor())\n",
    "test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor())"
   ],
   "id": "1a209cccb2d3a46e",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:42:52.534998Z",
     "start_time": "2024-12-01T11:42:52.528775Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train_loader=torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)\n",
    "test_loader=torch.utils.data.DataLoader(dataset=test_dataset, batch_size=64, shuffle=True)"
   ],
   "id": "a976eb3e7b2caa62",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:42:53.379979Z",
     "start_time": "2024-12-01T11:42:53.345293Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for x, y in train_loader:\n",
    "    print(y)\n",
    "    #print(x.view(x.shape[0], -1).shape)\n",
    "    #print(np.array(x.view(x.shape[0],-1)[0]).shape)\n",
    "    \n",
    "    break"
   ],
   "id": "93c2b0a551fc678e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([9, 9, 5, 6, 8, 5, 5, 3, 3, 4, 5, 0, 5, 2, 5, 4, 7, 1, 4, 6, 5, 1, 1, 4,\n",
      "        2, 6, 2, 1, 0, 5, 5, 9, 0, 5, 6, 0, 5, 8, 7, 0, 4, 7, 8, 4, 6, 8, 2, 8,\n",
      "        7, 3, 9, 5, 4, 8, 6, 8, 1, 6, 9, 6, 6, 4, 5, 6])\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:42:57.151371Z",
     "start_time": "2024-12-01T11:42:57.140833Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class Net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        self.fc1 = nn.Linear(28*28, 10)     #单层效果确实不好，但是确实方便:)\n",
    "        self.relu = nn.ReLU()\n",
    "        self.softmax = nn.LogSoftmax(dim=1)\n",
    "        \n",
    "    def forward(self, x):\n",
    "        x = self.fc1(x)\n",
    "        x = self.relu(x)\n",
    "        x = self.softmax(x)\n",
    "        return x\n",
    "    \n",
    "    def getweights(self):\n",
    "        return self.fc1.weight\n",
    "    "
   ],
   "id": "7e2559040656b577",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:52:44.931721Z",
     "start_time": "2024-12-01T11:52:44.915303Z"
    }
   },
   "cell_type": "code",
   "source": [
    "model = Net()\n",
    "criterion = nn.CrossEntropyLoss()\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n",
    "optimizerM = torch.optim.Adam(model.parameters(), lr=0.001,weight_decay=0.01)"
   ],
   "id": "7987af340187cdc7",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:52:46.927406Z",
     "start_time": "2024-12-01T11:52:46.919088Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def train(model, train_loader, optimizerM,epochs):\n",
    "    model.train()\n",
    "    for epoch in range(epochs):\n",
    "        for x, y in train_loader:\n",
    "            x=x.view(x.shape[0], -1) \n",
    "            output = model(x)\n",
    "            loss = criterion(output, y)\n",
    "            optimizerM.zero_grad()\n",
    "            loss.backward()\n",
    "            optimizerM.step()\n",
    "        print('Epoch: {}, Loss: {}'.format(epoch, loss.item()))"
   ],
   "id": "b833ef5525c1152a",
   "outputs": [],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:48:03.838398Z",
     "start_time": "2024-12-01T11:48:03.829693Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def lossL2(y_pred,y_true,model,lamda_1=0.01):\n",
    "    epsilon = 1e-15\n",
    "    y_true = torch.zeros_like(y_pred).scatter_(1, y_true.unsqueeze(1), 1)\n",
    "    y_pred = torch.clamp(y_pred, epsilon, 1.0 - epsilon)\n",
    "    weights = model.getweights()\n",
    "    lossW=torch.sum(torch.pow(weights,2))/2\n",
    "    loss = (torch.sum(y_true * torch.log(y_pred))+lossW*lamda_1) / y_true.size(0)\n",
    "    return abs(loss)"
   ],
   "id": "a1e28f4c4b02a555",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:48:05.713386Z",
     "start_time": "2024-12-01T11:48:05.705969Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def trainL2(model, train_loader, optimizer,epochs):\n",
    "    model.train()\n",
    "    for epoch in range(epochs):\n",
    "        for x, y in train_loader:\n",
    "            x=x.view(x.shape[0], -1) \n",
    "            output = model(x)\n",
    "            loss = lossL2(output,y,model)\n",
    "            optimizer.zero_grad()\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "        print('Epoch: {}, Loss: {}'.format(epoch, loss.item()))"
   ],
   "id": "10a396c322babff3",
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:54:41.016015Z",
     "start_time": "2024-12-01T11:52:50.448094Z"
    }
   },
   "cell_type": "code",
   "source": "train(model,train_loader,optimizerM,10)",
   "id": "77f3cdefe6932b8c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 0, Loss: 0.8922509551048279\n",
      "Epoch: 1, Loss: 0.6948211193084717\n",
      "Epoch: 2, Loss: 0.4421699643135071\n",
      "Epoch: 3, Loss: 0.29596251249313354\n",
      "Epoch: 4, Loss: 0.385059118270874\n",
      "Epoch: 5, Loss: 0.45780718326568604\n",
      "Epoch: 6, Loss: 0.37069445848464966\n",
      "Epoch: 7, Loss: 0.5052347183227539\n",
      "Epoch: 8, Loss: 0.4472733438014984\n",
      "Epoch: 9, Loss: 0.4677634835243225\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-01T11:50:02.414523Z",
     "start_time": "2024-12-01T11:48:08.103084Z"
    }
   },
   "cell_type": "code",
   "source": "trainL2(model,train_loader,optimizer,10)    #玩砸了:)",
   "id": "41ae8ead58dd016a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 0, Loss: 33.83147048950195\n",
      "Epoch: 1, Loss: 33.7945671081543\n",
      "Epoch: 2, Loss: 33.7315673828125\n",
      "Epoch: 3, Loss: 33.619728088378906\n",
      "Epoch: 4, Loss: 33.408668518066406\n",
      "Epoch: 5, Loss: 32.971378326416016\n",
      "Epoch: 6, Loss: 31.94179916381836\n",
      "Epoch: 7, Loss: 29.250696182250977\n",
      "Epoch: 8, Loss: 22.964256286621094\n",
      "Epoch: 9, Loss: 12.418590545654297\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "1818d4293bea9692"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
